#!/usr/bin/env zsh
 
# Terminal background is transparent or not
export TRANSPARENT=false

# FZF builtin key bindings and completion
[ -f ~/.config/fzf/fzf.zsh ] && source ~/.config/fzf/fzf.zsh

# Use fd instead of default find
export FZF_DEFAULT_COMMAND="fd --type f --hidden --follow $FD_EXCLUDE"

# Default options when running fzf
# Colors are consistent with my neovim colorscheme (see
# https://github.com/rockyzhang24/arctic.nvim inspired by VSCode Dark+)
# Keybindings:
# - ctrl-d: move cursor half page down
# - ctrl-u: move cursor halp page up
# - alt-d: move preview half page down
# - alt-u: move preview half page up
# - ctrl-a: toggle all
# - ctrl-/: toggle preview window
# - ctrl-o: open the selections via default application
# - ctrl-y: copy the selections to system clipboard
# - ctrl-e: edit the selections via $EDITOR
bg='#1f1f1f'
if $TRANSPARENT; then
  bg=-1
fi
export FZF_DEFAULT_OPTS=" \
  --multi \
  --reverse \
  --no-mouse \
  --height 60% \
  --scrollbar '█' \
  --history /tmp/fzfhistory \
  --prompt 'FZF> ' \
  --pointer '' \
  --marker ' ' \
  --tabstop=4 \
  --info 'inline:<' \
  --no-separator \
  --border 'top' \
  --bind 'ctrl-d:half-page-down,ctrl-u:half-page-up,alt-d:preview-half-page-down,`
    `alt-u:preview-half-page-up,ctrl-a:toggle-all,`
    `ctrl-/:toggle-preview,ctrl-o:execute(open {+}),`
    `ctrl-y:execute-silent(echo -n {+} | pbcopy),`
    `ctrl-e:execute(\$EDITOR {+} < /dev/tty > /dev/tty 2>&1),`
    `change:first' \
  --preview '~/.config/fzf/fzf-previewer.sh {}' \
  --preview-window 'border-left,<80(down,border-up)' \
  --color=dark,fg:#cccccc,preview-fg:#cccccc,bg:$bg,preview-bg:$bg,`
    `hl:#2aaaff,fg+:#ffffff,bg+:#04395e,gutter:$bg,hl+:#2aaaff,query:#cccccc,`
    `disabled:#808080,info:#8b949e,border:#454545,separator:#454545,`
    `scrollbar:#424242,label:#454545,preview-label:#454545,prompt:#cccccc,`
    `pointer:#ffffff,marker:#4ec9b0,spinner:#0078d4,header:#3794ff:italic:underline"

# Use fd for FZF completion
# 1). listing files and dirs ($1 is the base path to start search) such as "vim ~/.config/**<TAB>"
_fzf_compgen_path() {
  fd --hidden --follow $FD_EXCLUDE . "$1"
}
# 2). listing dirs such as "cd **<TAB>"
_fzf_compgen_dir() {
  fd --type d --hidden --follow $FD_EXCLUDE . "$1"
}

# CTRL-t to paste the selected files and directories onto the command-line
export FZF_CTRL_T_COMMAND="fd --hidden --follow $FD_EXCLUDE"
export FZF_CTRL_T_OPTS=" \
  --header 'Paste the selected files and directories onto the command-line'"

# CTRL-r to search the command history
export FZF_CTRL_R_OPTS=" \
  --preview 'echo {}' \
  --preview-window down:3:wrap,border-up \
  --bind 'ctrl-y:execute-silent(echo -n {2..} | pbcopy)' \
  --header 'Press CTRL-Y to copy command into clipboard'"

# CTRL-j to cd into the selected dir
bindkey '^j' fzf-cd-widget
export FZF_ALT_C_COMMAND="fd --type d --hidden --follow $FD_EXCLUDE"
export FZF_ALT_C_OPTS=" \
  --preview 'tree -C {} | head -200' --header 'cd into the selected directory'"

# Utility commands (autoload functions defined under ~/.config/fzf/fzfutils/)
fpath+=${0:h}/fzfutils
autoload -Uz ${0:h}/fzfutils/*(.:t)

# fzf + git
# Reference: https://github.com/junegunn/fzf-git.sh
# CTRL-G F for files
# CTRL-G B for branches
# CTRL-G T for tags
# CTRL-G R for remotes
# CTRL-G H for commit hashes
# CTRL-G S for stashes
# CTRL-G E for Each ref (git for-each-ref)
source ~/.config/fzf/fzf-git.sh
